
package com.platform.modules.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.Constant;
import com.platform.common.utils.RestResponse;
import com.platform.modules.mall.controller.dto.MallCardShopsDto;
import com.platform.modules.mall.entity.MallGoodsEntity;
import com.platform.modules.mall.entity.MallShopsGoodsEntity;
import com.platform.modules.mall.service.IAssociTagDataChange;
import com.platform.modules.mall.service.MallAssociTagService;
import com.platform.modules.mall.service.MallGoodsService;
import com.platform.modules.mall.service.MallShopsGoodsService;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.service.SysUserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 店铺与商品关联关系Controller
 *
 * @author
 * @date 2019-07-04 09:46:59
 */
@RestController
@RequestMapping("mall/shopsgoods")
public class MallShopsGoodsController extends AbstractController {
    @Autowired
    private MallShopsGoodsService mallShopsGoodsService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Resource
    private MallAssociTagService associTagService;
    @Resource
    private IAssociTagDataChange shopsGoodsTag;
    @Resource
    private MallGoodsService goodsService;


/**
 * 新增卡卷与店铺关联
 *
 * @param mallCardShopsDto mallCardShopsDtos
 * @return RestResponse
 */
    @SysLog("新增卡卷与店铺关联")
    @RequestMapping("/saveCardShops")
    @RequiresPermissions("mall:goods:save")
    @Transactional
    public RestResponse saveShopsGoods(@RequestBody MallCardShopsDto mallCardShopsDto) {

        // 删除标签
        List<String> shopsIds = mallShopsGoodsService.listObjs(new LambdaQueryWrapper<MallShopsGoodsEntity>()
                .select(MallShopsGoodsEntity::getShopsId).eq(MallShopsGoodsEntity::getGoodsId,mallCardShopsDto.getGoodsId()),Object::toString);
        associTagService.delateByOtherId(mallCardShopsDto.getGoodsId(),shopsIds.stream().filter(x->!mallCardShopsDto.getShopsIds().contains(x)).toArray(String[]::new));

        mallShopsGoodsService.remove(new LambdaQueryWrapper<MallShopsGoodsEntity>().eq(MallShopsGoodsEntity::getGoodsId,mallCardShopsDto.getGoodsId()));

        MallGoodsEntity goodsEntity = goodsService.getById(mallCardShopsDto.getGoodsId());
        List<MallShopsGoodsEntity> entities = new ArrayList<>();
        MallShopsGoodsEntity entity;
        for (String shopsId : mallCardShopsDto.getShopsIds()) {
            entity = new MallShopsGoodsEntity();
            entity.setGoodsId(mallCardShopsDto.getGoodsId());
            entity.setShopsId(shopsId);
            entities.add(entity);

            // 添加店铺标签
            if(!shopsIds.contains(shopsId)){
                shopsGoodsTag.add(shopsId,goodsEntity);
            }
        }
        mallShopsGoodsService.saveBatch(entities);

        return RestResponse.success();
    }

    /**
     * 获取商品关联的店铺
     *
     * @param goodsId 查询参数
     * @return RestResponse
     */
    @RequestMapping("/getGoodsShops")
    @RequiresPermissions("mall:shopsgoods:list")
    public RestResponse getGoodsShops(@RequestParam String goodsId) {
        List<MallShopsGoodsEntity> list = mallShopsGoodsService.lambdaQuery()
                .select(MallShopsGoodsEntity::getShopsId,MallShopsGoodsEntity::getShopsId)
                .eq(MallShopsGoodsEntity::getGoodsId,goodsId)
                .list();
        return RestResponse.success().put("list", list);
    }

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("mall:shopsgoods:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<MallShopsGoodsEntity> list = mallShopsGoodsService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询店铺与商品关联关系
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("mall:shopsgoods:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {

        //获取用户所属的角色列表
        List<String> roleIdList = sysUserRoleService.queryRoleIdList(this.getUserId());
        if (roleIdList.contains(Constant.ORG_ADMIN)) {
            params.put("orgNo", this.getOrgNo());
        } else if (!(this.getUserId().equals(Constant.SUPER_ADMIN) || roleIdList.contains(Constant.ADMIN))) {
            params.put("userId", this.getUserId());
        }
        Page page = mallShopsGoodsService.queryPage(params);


        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:shopsgoods:info")
    public RestResponse info(@PathVariable("id") String id) {
        MallShopsGoodsEntity mallShopsGoods = mallShopsGoodsService.getById(id);

        return RestResponse.success().put("shopsgoods", mallShopsGoods);
    }

    /**
     * 新增店铺与商品关联关系
     *
     * @param mallShopsGoods mallShopsGoods
     * @return RestResponse
     */
    @SysLog("新增店铺与商品关联关系")
    @RequestMapping("/save")
    @RequiresPermissions("mall:shopsgoods:save")
    public RestResponse save(@RequestBody MallShopsGoodsEntity mallShopsGoods) {

        mallShopsGoodsService.add(mallShopsGoods);

        return RestResponse.success();
    }

    /**
     * 修改店铺与商品关联关系
     *
     * @param mallShopsGoods mallShopsGoods
     * @return RestResponse
     */
    @SysLog("修改店铺与商品关联关系")
    @RequestMapping("/update")
    @RequiresPermissions("mall:shopsgoods:update")
    public RestResponse update(@RequestBody MallShopsGoodsEntity mallShopsGoods) {

        mallShopsGoodsService.update(mallShopsGoods);

        return RestResponse.success();
    }

    /**
     * 根据主键删除店铺与商品关联关系
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除店铺与商品关联关系")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:shopsgoods:delete")
    public RestResponse delete(@RequestBody String[] ids) {

//        for(String id : ids){
//            MallShopsGoodsEntity entity = mallShopsGoodsService.getById(id);
//            // 删除店铺的商品标签
//            if(entity != null){
//                associTagService.delateByAssoci(entity.getShopsId(),entity.getGoodsId());
//            }
//        }
        shopsGoodsTag.delate(mallShopsGoodsService.listObjs(new LambdaQueryWrapper<MallShopsGoodsEntity>()
                .select(MallShopsGoodsEntity::getGoodsId).in(MallShopsGoodsEntity::getId,ids),Object::toString).stream().toArray(String[]::new));

        mallShopsGoodsService.deleteBatch(ids);


        return RestResponse.success();
    }
}
